package com.ruoyi.campus.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 文件存储配置
 * 
 * @author campus-alarm
 * @date 2024-01-01
 */
@Configuration
@EnableAsync
public class FileStorageConfig
{
    /**
     * 异步任务执行器 - 用于媒体文件处理
     */
    @Bean("mediaProcessTaskExecutor")
    public Executor mediaProcessTaskExecutor()
    {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("media-process-");
        executor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    /**
     * 异步任务执行器 - 用于文件上传
     */
    @Bean("fileUploadTaskExecutor")
    public Executor fileUploadTaskExecutor()
    {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(8);
        executor.setQueueCapacity(500);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("file-upload-");
        executor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    /**
     * 文件存储属性配置
     */
    @Bean
    @ConfigurationProperties(prefix = "campus.file")
    public FileProperties fileProperties()
    {
        return new FileProperties();
    }

    public static class FileProperties
    {
        private String storageType = "local";
        private String localPath = System.getProperty("user.home") + "/campus-files";
        private String accessPrefix = "/campus-files";
        private OssProperties oss = new OssProperties();
        private QCloudProperties qcloud = new QCloudProperties();
        private QiniuProperties qiniu = new QiniuProperties();

        public static class OssProperties
        {
            private String endpoint;
            private String accessKeyId;
            private String accessKeySecret;
            private String bucketName;
            private String domain;

            // getters and setters
            public String getEndpoint() { return endpoint; }
            public void setEndpoint(String endpoint) { this.endpoint = endpoint; }
            public String getAccessKeyId() { return accessKeyId; }
            public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; }
            public String getAccessKeySecret() { return accessKeySecret; }
            public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; }
            public String getBucketName() { return bucketName; }
            public void setBucketName(String bucketName) { this.bucketName = bucketName; }
            public String getDomain() { return domain; }
            public void setDomain(String domain) { this.domain = domain; }
        }

        public static class QCloudProperties
        {
            private String secretId;
            private String secretKey;
            private String region;
            private String bucketName;
            private String domain;

            // getters and setters
            public String getSecretId() { return secretId; }
            public void setSecretId(String secretId) { this.secretId = secretId; }
            public String getSecretKey() { return secretKey; }
            public void setSecretKey(String secretKey) { this.secretKey = secretKey; }
            public String getRegion() { return region; }
            public void setRegion(String region) { this.region = region; }
            public String getBucketName() { return bucketName; }
            public void setBucketName(String bucketName) { this.bucketName = bucketName; }
            public String getDomain() { return domain; }
            public void setDomain(String domain) { this.domain = domain; }
        }

        public static class QiniuProperties
        {
            private String accessKey;
            private String secretKey;
            private String bucketName;
            private String domain;

            // getters and setters
            public String getAccessKey() { return accessKey; }
            public void setAccessKey(String accessKey) { this.accessKey = accessKey; }
            public String getSecretKey() { return secretKey; }
            public void setSecretKey(String secretKey) { this.secretKey = secretKey; }
            public String getBucketName() { return bucketName; }
            public void setBucketName(String bucketName) { this.bucketName = bucketName; }
            public String getDomain() { return domain; }
            public void setDomain(String domain) { this.domain = domain; }
        }

        // getters and setters
        public String getStorageType() { return storageType; }
        public void setStorageType(String storageType) { this.storageType = storageType; }
        public String getLocalPath() { return localPath; }
        public void setLocalPath(String localPath) { this.localPath = localPath; }
        public String getAccessPrefix() { return accessPrefix; }
        public void setAccessPrefix(String accessPrefix) { this.accessPrefix = accessPrefix; }
        public OssProperties getOss() { return oss; }
        public void setOss(OssProperties oss) { this.oss = oss; }
        public QCloudProperties getQcloud() { return qcloud; }
        public void setQcloud(QCloudProperties qcloud) { this.qcloud = qcloud; }
        public QiniuProperties getQiniu() { return qiniu; }
        public void setQiniu(QiniuProperties qiniu) { this.qiniu = qiniu; }
    }
} 